package com.reremouse.rerebbs.web.mb;

import com.reremouse.rerebbs.dao.IBbsTalkDAO;
import com.reremouse.rerebbs.dao.IBbsThreadDAO;
import com.reremouse.rerebbs.dao.IBbsUserDAO;
import com.reremouse.rerebbs.model.BBSScoreRegular;
import com.reremouse.rerebbs.model.BbsTalk;
import com.reremouse.rerebbs.model.BbsThread;
import com.reremouse.rerebbs.model.BbsUser;
import com.reremouse.util.SpringHelper;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@ManagedBean
@ViewScoped
public class ThreadDetail
        implements Serializable {
    BbsTalk newTalk = new BbsTalk();
    BbsThread thread = new BbsThread();
    IBbsThreadDAO bbsThreadDAO = (IBbsThreadDAO) SpringHelper.getSpringBean("BbsThreadDAO");
    IBbsTalkDAO bbsTalkDAO = (IBbsTalkDAO) SpringHelper.getSpringBean("BbsTalkDAO");
    @ManagedProperty("#{clientSession}")
    ClientSession cs;
    IBbsUserDAO userDAO = (IBbsUserDAO) SpringHelper.getSpringBean("BbsUserDAO");
    int pageShow = 1;
    BbsTalk talk_temp = new BbsTalk();
    int pageCount = 0;
    int talkRows = 10;
    List<BbsTalk> talks = new ArrayList();

    public int getPageShow() {
        return this.pageShow;
    }

    public void setPageShow(int pageShow) {
        this.pageShow = pageShow;
    }

    public BbsTalk getNewTalk() {
        return this.newTalk;
    }

    public int getPageCount() {
        return this.pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public int getTalkRows() {
        return this.talkRows;
    }

    public void setTalkRows(int talkRows) {
        this.talkRows = talkRows;
    }

    public void setNewTalk(BbsTalk newTalk) {
        this.newTalk = newTalk;
    }

    public BbsThread getThread() {
        return this.thread;
    }

    public void setThread(BbsThread thread) {
        this.thread = thread;
    }

    public BbsTalk getTalk_temp() {
        return this.talk_temp;
    }

    public void setTalk_temp(BbsTalk talk_temp) {
        this.talk_temp = talk_temp;
    }

    public List<BbsTalk> getTalks() {
        return this.talks;
    }

    public void setTalks(List<BbsTalk> talks) {
        this.talks = talks;
    }

    public ClientSession getCs() {
        return this.cs;
    }

    public void setCs(ClientSession cs) {
        this.cs = cs;
    }

    @PostConstruct
    public void init() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String temp = request.getParameter("id");
        if (temp != null) {
            this.thread = this.bbsThreadDAO.findById(Long.valueOf(Long.parseLong(temp.trim())));
            this.thread.setReadCount(Integer.valueOf(this.thread.getReadCount().intValue() + 1));
            this.bbsThreadDAO.update(this.thread);
            synDB();
            countPage();
        }
    }

    private void countPage() {
        int c = this.talks.size();
        int p = c / this.talkRows;
        int m = c % this.talkRows;
        if (m == 0) {
            this.pageCount = p;
        } else {
            this.pageCount = (p + 1);
        }
    }

    private void synDB() {
        this.talks = null;
        this.talks = this.bbsTalkDAO.findByThread(this.thread, new int[0]);
    }

    public String addNewTalk() {
        addNewTalk(null);
        return null;
    }

    public void addNewTalk(AjaxBehaviorEvent event) {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        BbsUser bu = this.cs.getUsr();
        bu.setScore(bu.getScore() + BBSScoreRegular.NEW_TALK);
        this.userDAO.updateBbsUser(bu);
        this.newTalk.setGenTime(new Date());
        this.newTalk.setGenBy(bu);
        this.newTalk.setThread(this.thread);
        this.newTalk.setLastEditTime(new Date());
        this.newTalk.setIp(request.getRemoteAddr());
        this.bbsTalkDAO.save(this.newTalk);
        this.thread.setLatestTalk(this.newTalk);
        this.bbsThreadDAO.update(this.thread);
        this.talks.add(this.newTalk);
        this.newTalk = new BbsTalk();
        countPage();
    }

    public void toSomeone(String u) {
        this.newTalk.setContent("To " + u + " #&nbsp;<br/>");
    }

    public void quote(String t) {
        if (t.length() > 50) {
            t = t.substring(0, 49) + "...";
        }
        t = t.replaceAll("<p>", "");
        t = t.replaceAll("</p>", "");
        this.newTalk.setContent(t + "<br/>--------------------<br/>");
    }

    public void block(long id) {
        BbsTalk t = this.bbsTalkDAO.findById(Long.valueOf(id));
        t.setIfPub(Boolean.valueOf(false));
        this.bbsTalkDAO.update(t);
        for (BbsTalk s : this.talks) {
            if (s.getId().equals(t.getId())) {
                s.setIfPub(Boolean.valueOf(false));
                break;
            }
        }
    }

    public void unblock(long id) {
        BbsTalk t = this.bbsTalkDAO.findById(Long.valueOf(id));
        t.setIfPub(Boolean.valueOf(true));
        this.bbsTalkDAO.update(t);
        for (BbsTalk s : this.talks) {
            if (s.getId().equals(t.getId())) {
                s.setIfPub(Boolean.valueOf(true));
                break;
            }
        }
    }

    public void del(long id) {
        BbsTalk t = this.bbsTalkDAO.findById(Long.valueOf(id));
        BbsTalk st = null;
        for (BbsTalk s : this.talks) {
            if (s.getId().equals(t.getId())) {
                st = s;
                System.out.println("deleted.");
                break;
            }
        }
        this.talks.remove(st);
        this.bbsTalkDAO.delete(t);
    }

    public void del(AjaxBehaviorEvent event) {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String id = request.getParameter("tid");

        BbsTalk t = this.bbsTalkDAO.findById(Long.valueOf(Long.parseLong(id)));

        BbsTalk st = null;
        for (BbsTalk s : this.talks) {
            if (s.getId().equals(t.getId())) {
                st = s;
                System.out.println("deleted.");
            }
        }
        this.talks.remove(st);
        this.bbsTalkDAO.delete(t);
        System.out.println(this.talks.size());
    }

    public void edit(long id) {
        BbsTalk t = this.bbsTalkDAO.findById(Long.valueOf(id));
        setTalk_temp(t);
    }

    public String edit_ok() {
        this.talk_temp.setLastEditTime(new Date());
        this.bbsTalkDAO.update(this.talk_temp);

        setTalks(this.bbsTalkDAO.findByThread(this.thread, new int[0]));
        return null;
    }
}



/* Location:           F:\jsp源码\蝙蝠论坛系统\rerebbs\WEB-INF\classes\

 * Qualified Name:     com.reremouse.rerebbs.web.mb.ThreadDetail

 * JD-Core Version:    0.7.0.1

 */